package com.wfs.myapp.repository.bk;

import com.wfs.myapp.entity.domain.bk.BkSearchTop;
import com.wfs.myapp.entity.vo.SearchVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.naming.directory.SearchControls;
import java.util.List;

@Repository
public interface SearchTopRepository extends JpaRepository<BkSearchTop, Long> {

    @Query(nativeQuery = true, value =
            " SELECT 1 type, u.id, u.nickname name, u.avatar image " +
                    " FROM bk_search_top st " +
                    " LEFT JOIN user u ON st.search_id = u.id " +
                    " WHERE st.type = :type " +
                    " ORDER BY st.number desc " +
                    " LIMIT :page, :size "
            )
    List<SearchVo.SearchItem> findUserTop(Integer type, Integer page, Integer size);

    @Query(nativeQuery = true, value =
            " SELECT count(*) " +
                    " FROM bk_search_top st " +
                    " LEFT JOIN bk_book b ON st.search_id = b.id " +
                    " WHERE st.type = :type " +
                    " ORDER BY st.number desc "
    )
    long countBookTop(Integer type);
    @Query(nativeQuery = true, value =
            " SELECT 2 type, b.id, b.book_name name, b.book_avatar image, b.author author " +
                    " FROM bk_search_top st " +
                    " LEFT JOIN bk_book b ON st.search_id = b.id " +
                    " WHERE st.type = :type " +
                    " ORDER BY st.number desc " +
                    " LIMIT :page, :size "
    )
    List<SearchVo.SearchItem> findBookTop(Integer type, Integer page, Integer size);

    BkSearchTop findByTypeAndSearchId(Integer type, Long searchId);

}

